<template>
  <div class="app-container">
    <search-bar :form="queryParams" @sub="getList"></search-bar>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button @click="handleAdd" class="asset">新增单据</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain @click="formPrint">打印</el-button>
      </el-col>
      <right-toolbar @queryTable="Refresh"></right-toolbar>
    </el-row>
    <vxe-table ref="table" :data="tableData.list">
      <vxe-column type="checkbox" width="50" align="center" />
      <vxe-column width="100" title="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="apply_state" :value="scope.row.state" />
        </template>
      </vxe-column>
      <vxe-column title="编号" field="number">
        <template #default="{row}">
          <el-button style="margin-right: 6px;" link type="primary" size="small" @click="handleDetail(row.id)">
            {{ row.number }}
          </el-button>
        </template>
      </vxe-column>

      <vxe-column title="转入公司" field="companyName"></vxe-column>
      <vxe-column title="转入管理员" field="adminName"></vxe-column>
      <vxe-column title="数量" field="num"></vxe-column>
      <vxe-column title="申请人" field="createUserName"></vxe-column>
      <vxe-column title="备注" field="remark"></vxe-column>

      <vxe-column title="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">

          <el-button style="margin-right: 10px;" link type="primary" size="small"
            v-if="row.state === 0 || row.state === 3" @click="handleSub(row)">提交</el-button>
          <el-button style="margin-right: 10px;" link type="primary" size="small"
            v-if="row.state === 0 || row.state === 3" @click="handleUpdate(row)">修改</el-button>
          <el-button v-if="row.state === 0 || row.state === 3" size="small" link type="primary"
            @click="handleDelete(scope.row)">删除</el-button>
          <el-button v-if="row.state !== 0 && row.state !== 3" size="small" link type="primary"
                     @click="handleDetail(row.id)">审批详情</el-button>
          <el-button v-if="row.state !== 0 && row.state !== 3" size="small" link type="primary"
                     @click="formPrintOne(row)">打印</el-button>

        </template>
      </vxe-column>
    </vxe-table>
    <div class="pagination-container">
      <pagination v-show="tableData.paging.total>0" :total="tableData.paging.total"
        v-model:page="tableData.paging.pageNum" v-model:limit="tableData.paging.pageSize" @pagination="getList" />
    </div>

    <print-dialog ref="printDialog" @sub="getList"></print-dialog>
    <edit :apply-type-name="applyTypeName" :apply-type="applyType" ref="edit" @sub="getList"></edit>
    <detail ref="detailDialog"></detail>
  </div>

</template>
<script setup>
import api from "./api.js";
import Edit from './edit.vue'
import SearchBar from "./SearchBar.vue";
import Detail from "./detail/index.vue";
import PrintDialog from './PrintDialog.vue'
const edit = ref();

const printDialog = ref();
const detailDialog = ref();
const table = ref();

const { proxy } = getCurrentInstance();
const { apply_state } = proxy.useDict("apply_state");

// 申请类型
const applyType = 'ZCDB';
const applyTypeName = '资产调拨';

const queryParams = reactive({
  applyType: applyType,
});

const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
  paging: {
    total: 0, // 总条数
    pageNum: 1,
    pageSize: 10,
  }
});

function getList() {
  tableData.loading = true;
  const {pageNum, pageSize} = tableData.paging;

  const query = {...queryParams};
  const pageReq = { page: pageNum - 1, size: pageSize };
  api.list(query, pageReq).then(response => {
    const { content, totalElements } = response
    if (content && content.length > 0) {
      content.forEach(item => {
        debugger
        if (item.formData) {
          let formData = JSON.parse(item.formData);
          Object.assign(item, formData);
        }
      })
    }

    tableData.list = content;
    tableData.paging.total = totalElements;
    tableData.loading = false;

  });
}

function Refresh() {
  proxy.$message.success("刷新成功");
}
function handleAdd() {
  edit.value.open();
}

function handleUpdate(row) {
  edit.value.open(row);
}
function formPrint() {
  const selectedList = table.value.getCheckboxRecords();
  if (selectedList.length === 0) {
    proxy.$message.warning('请选择需要打印的项');
    return;
  }
  selectedList.forEach(item => {
    window.open(`./#formPrint/${applyType}/${item.number}`);
  })
}

function formPrintOne(item) {
  window.open(`./#formPrint/${applyType}/${item.number}`);
}

function handleDetail(id) {
  detailDialog.value.open(id);
}

function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除').then(function() {
    return api.del(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

function handleSub(row) {
  api.sub(row.id).then(()=> {
    this.getList();
  });
}

onMounted(()=> {
  getList()
})

</script>
  
<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  padding: 8px;
  height: 92vh; 
  overflow: hidden; 
}
vxe-row {
  height: 300px;
}
.vxe-table {
  flex: 1;
  overflow: auto;
}
.asset {
  width: 74px;
  height: 35px;
  background-color: #005bf5;
  color: #fff;
  font-size: 12px;
  border-radius: 8%;
}

.pagination-container {
  display: flex;
  justify-content: flex-end; 

}
</style>